﻿// 基础UI窗体     
// 功能：所有用户UI窗体的父类
// 1：定于四个“UI窗体”的状态
// OnInit:      显示状态
// OnEnter:     显示状态
// OnExit:      隐藏状态(即：不能看见，不能操作)
// OnReEnter:   重新显示状态
// OnFreeze:    冻结状态(即：在其他窗体下面，看见但不能操作)

using UnityEngine;

namespace MotionFramework.UIKit
{
    public class UIWindow : UIMediator
    {
        /// <summary>
        /// 当前(基类)窗口的类型
        /// </summary>
        private UIType _CurrentUIType = new UIType();

        /// <summary>
        /// 属性_当前UI窗体类型
        /// </summary>
        public UIType CurrentUIType => _CurrentUIType;

        [Header("是否需要清空反向切换")]
        [SerializeField]
        bool IsClearReverseChange = false;
        [Header("UI窗体类型")]
        [SerializeField]
        UIWindowType UIWindow_Type = UIWindowType.Normal;
        [Header("UI窗体显示类型")]
        [SerializeField]
        UIWindowShowMode UIWindow_ShowMode = UIWindowShowMode.Normal;
        [Header("UI窗体透明度类型")]
        [SerializeField]
        UIWindowLucencyType UIWindow_LucencyType = UIWindowLucencyType.Lucency;

        #region 窗体生命周期

        protected override void Awake()
        {
            base.Awake();

            _CurrentUIType.IsClearReverseChange = IsClearReverseChange;
            _CurrentUIType.UIWindow_Type = UIWindow_Type;
            _CurrentUIType.UIWindow_ShowMode = UIWindow_ShowMode;
            _CurrentUIType.UIWindow_LucencyType = UIWindow_LucencyType;
        }

        /// <summary>
        /// 初始化窗体
        /// </summary>
        protected virtual void OnInit() {}

        /// <summary>
        /// 打开窗体
        /// </summary>
        protected virtual void OnEnter() {}

        /// <summary>
        /// 重新打开窗体
        /// </summary>
        protected virtual void OnReEnter() {}

        /// <summary>
        /// 关闭窗体
        /// </summary>
        protected virtual void OnExit() {}

        /// <summary>
        /// 冻结窗体
        /// </summary>
        protected virtual void OnFreeze() {}

        //初始化
        internal void Init()
        {
            OnInit();
        }

        //页面显示
        internal void Enter()
        {
            this.gameObject.SetActive(true);
            if (_CurrentUIType.UIWindow_Type == UIWindowType.PopUp 
            || _CurrentUIType.UIWindow_Type == UIWindowType.TopUp) 
            {
                //添加UI遮罩处理
                UIMaskMgr.GetInstance().SetMaskWindow(gameObject, _CurrentUIType.UIWindow_LucencyType);                
            }

            // 自动注册MVC View
            RegisterViewCommand();
            OnEnter();
        }

        //页面隐藏(不在“栈”集合中)
        internal void Exit()
        {
            this.gameObject.SetActive(false);
            if (_CurrentUIType.UIWindow_Type == UIWindowType.PopUp
            || _CurrentUIType.UIWindow_Type == UIWindowType.TopUp)
            {
                //添加UI遮罩处理
                UIMaskMgr.GetInstance().CancleMaskWindow();
            }

            // 自动销毁MVC View
            DestroyViewCommand();          
            OnExit();
        }

        //页面重新显示
        internal void ReEnter()
        {
            this.gameObject.SetActive(true);
            if (_CurrentUIType.UIWindow_Type == UIWindowType.PopUp
            || _CurrentUIType.UIWindow_Type == UIWindowType.TopUp)
            {
                //添加UI遮罩处理
                UIMaskMgr.GetInstance().SetMaskWindow(gameObject, _CurrentUIType.UIWindow_LucencyType);
            }

            OnReEnter();
        }

        //页面冻结(还在“栈”集合中)
        internal void Freeze()
        {
            this.gameObject.SetActive(true);
            OnFreeze();
        } 
        #endregion

        #region 给子类封装的方法

        /// <summary>
        /// 打开UI界面 - 名称
        /// </summary>
        protected void ShowUIWindow(string name)
        {
            UIManager.GetInstance().ShowUIWindow(name);
        }

        /// <summary>
        /// 打开UI界面 - 模板
        /// </summary>
        protected void ShowUIWindow<T>() where T : UIWindow
        {
            UIManager.GetInstance().ShowUIWindow(typeof(T).Name);
        }

        /// <summary>
        /// 关闭UI界面 - 名称
        /// </summary>
        protected void CloseUIWindow(string name)
        {
            UIManager.GetInstance().CloseUIWindow(name);
        }

        /// <summary>
        /// 关闭UI界面 - 模板
        /// </summary>
        protected void CloseUIWindow<T>() where T : UIWindow
        {
            UIManager.GetInstance().CloseUIWindow(typeof(T).Name);
        }

        /// <summary>
        /// 显示语言信息
        /// </summary>
        /// <param name="text"></param>
        protected string ShowText(string text)
        {
            return LauguageMgr.GetInstance().ShowText(text);
        }

        #endregion

    }//Class_end
}
